﻿using Growatt.Component.Frm;
using Growatt.WaveData.DataSetResolve;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Vosc.VirtualOscilloscope;


namespace Growatt.WaveData.VirtualOscilloscope
{
    public partial class UC_BtnAddDelControl : UserControl
    {
        private readonly UC_ParameterSetting cuurentSelectItem;
        UIComboDataGridView uIListBox = null;

        /// <summary>
        /// 删除回调函数
        /// </summary>
        Action<ModelSaveString> ActionDelData = null;

        public UC_BtnAddDelControl(UC_ParameterSetting cuurentSelectItem,UIComboDataGridView uIlb, BindingList<ModelSaveString> modelSaveStrings, 
            List<ModelSaveString> modelSave, Action<ModelSaveString> action)
        {
            InitializeComponent();
            this.cuurentSelectItem = cuurentSelectItem;
            this.uIListBox = uIlb;
            ActionDelData = action;
            //tableLayoutPanel1.Controls.Add(uIListBox, 0, 0);
        }
        /// <summary>
        /// 是否包含中文
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool hasChinese(string source)
        {
            return Regex.IsMatch(source, @"[\u4e00-\u9fa5]+", RegexOptions.IgnoreCase);
        }
        private void addBtn_Click(object sender, EventArgs e)
        {

            FrmFindAddress frmFindAddress = new FrmFindAddress(this.cuurentSelectItem.cuurentSelectItem, (o) =>
            {
                AddIntemUIControl((o.ComboBoxItem, o.TypeContent, o.ScaleContent, o.AddressContent));
            });
            frmFindAddress.ShowDialog();

        }

        public Action<(string, string, string, string)> AddAction = null;
        public void AddIntemUIControl((string, string, string, string) name)
        {
            var datas = OscilloscopeControl.modelSaveStrings.FirstOrDefault(o => o.Name.Equals(name.Item1)
            && o.Type.Equals(name.Item2)
            && o.Scale.Equals(name.Item3)
            && o.Address.Equals(name.Item4)
            );
            if (datas == null)
            {
                ModelSaveString modelSaveString = new ModelSaveString();
                modelSaveString.Name = name.Item1;
                modelSaveString.Type = name.Item2;
                modelSaveString.Scale = name.Item3;
                modelSaveString.Address = name.Item4;
                OscilloscopeControl.modelSaveStrings.Insert(0, modelSaveString);
            }
            AddAction?.Invoke(name);
            OscilloscopeControl.BindingListmodelSaveStrings.ResetBindings();

        }
        public void RemoveUIControl(UIComboDataGridView uiListBox1)
        {
            //var datas = OscilloscopeControl.modelSaveStrings.FirstOrDefault(o => o.Name.Equals(uiListBox1.Text));
            //if (datas!=null)
            //{
            //    if (OscilloscopeControl.modelSaveStrings.Remove(datas)) ;
            //    BindingListmodelSaveStrings.Remove(datas);

            //}
            ActionDelData?.Invoke(null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RemoveUIControl(uIListBox);
        }
        /// <summary>
        /// 获取文本框的内容
        /// </summary>
        /// <returns></returns>
        public string GetString()
        {
            return uIListBox.Text.Replace(";", "");
        }

        /// <summary>
        /// 获取文本框的内容
        /// </summary>
        /// <returns></returns>
        public void setString(string Setval)
        {
            uIListBox.Text = Setval;
        }
    }
}
